﻿using System;

namespace UltimateNavigationFramework.Helpers
{
    /// <summary>
    /// Extension to the prism's UriParsingHelper
    /// </summary>
    internal static class UriParser
    {
        static UriParser()
        {
        }

        internal static bool InternalUriIsFragment(Uri uri)
        {
            if (uri != (Uri)null && !uri.IsAbsoluteUri && !string.IsNullOrEmpty(uri.OriginalString))
                return uri.OriginalString.StartsWith("#", StringComparison.Ordinal);
            else
                return false;
        }

        internal static string InternalUriGetAllButFragment(Uri uri)
        {
            return UriParser.GetUriComponents(uri, UriComponents.Path | UriComponents.Query);
        }

        internal static string InternalUriToExternalValue(Uri uri)
        {
            Uri uri1 = MakeAbsolute(uri);
            UriComponents components = UriComponents.Path;

            if (uri != (Uri)null && uri.OriginalString.StartsWith("/", StringComparison.Ordinal))
                components |= UriComponents.KeepDelimiter;

            string str1 = uri1.GetComponents(components, UriFormat.UriEscaped).Replace("$"[0].ToString(), "%24");
            string str2 = uri1.GetComponents(UriComponents.Query, UriFormat.UriEscaped).Replace("$"[0].ToString(), "%24");
            string str3 = uri1.GetComponents(UriComponents.Fragment, UriFormat.UriEscaped).Replace("$"[0].ToString(), "%24");
            string str4 = str1;

            if (!string.IsNullOrEmpty(str2))
                str4 = str4 + "?" + str2;
            if (!string.IsNullOrEmpty(str3))
                str4 = str4 + "$" + str3;

            return str4;
        }

        internal static string InternalUriFromExternalValue(string externalVal)
        {
            string str = MakeAbsolute(externalVal.Replace("$", "#").Replace("%24", "$"))
                            .GetComponents(UriComponents.Path | UriComponents.Query | UriComponents.Fragment, UriFormat.SafeUnescaped);

            if (!externalVal.StartsWith("/", StringComparison.Ordinal))
                str = str.Substring(1);

            return str;
        }

        internal static Uri MakeAbsolute(Uri baseUri)
        {
            if (baseUri == (Uri)null || baseUri.OriginalString.StartsWith("/", StringComparison.Ordinal))
                return new Uri("http://localhost" + (object)baseUri, UriKind.Absolute);
            else
                return new Uri("http://localhost/" + (object)baseUri, UriKind.Absolute);
        }

        internal static Uri MakeAbsolute(string originalString)
        {
            if (originalString == null || originalString.StartsWith("/", StringComparison.Ordinal))
                return new Uri("http://localhost" + originalString, UriKind.Absolute);
            else
                return new Uri("http://localhost/" + originalString, UriKind.Absolute);
        }

        internal static Uri EnsureAbsolute(Uri uri)
        {
            if (uri.IsAbsoluteUri)
            {
                return uri;
            }

            if ((uri != null) && !uri.OriginalString.StartsWith("/", StringComparison.Ordinal))
            {
                return new Uri("http://localhost/" + uri, UriKind.Absolute);
            }
            return new Uri("http://localhost" + uri, UriKind.Absolute);
        }

        private static string GetUriComponents(Uri uri, UriComponents components)
        {
            if (uri != (Uri)null && string.IsNullOrEmpty(uri.OriginalString))
                return string.Empty;
            if (uri != (Uri)null && uri.OriginalString.StartsWith("/", StringComparison.Ordinal))
                components |= UriComponents.KeepDelimiter;
            return UriParser.MakeAbsolute(uri).GetComponents(components, UriFormat.SafeUnescaped);
        }
    }
}
